#include"../include/lib_headers.h"
//*****************************************************************************************//
//*************************************FUNCTIONS*******************************************//
void insert(fd *data) {
	b_node *newNode = (b_node *) malloc(sizeof(b_node));
	b_node* temp;
	newNode->fileDesc = data;
	newNode->left = NULL;
	newNode->right = NULL;
	newNode->parent = NULL;
	if (bstRoot == NULL ) {
		bstRoot = newNode;
	} else {
		temp = bstRoot;
		while (1) { 
			/*
			printf("newnode->fileDesc->fullPath = %s\n", newNode->fileDesc->fullPath);
			printf("temp->fileDesc->fullPath = %s\n", temp->fileDesc->fullPath);
												*/
			if (strcmp(newNode->fileDesc->fullPath, temp->fileDesc->fullPath)
					< 0) {
				if (temp->left == NULL ) {
					newNode->parent = temp;
					temp->left = newNode;
					break;
				} else {
					temp = temp->left;
				}
			} else if (strcmp(newNode->fileDesc->fullPath,
					temp->fileDesc->fullPath) > 0) {
				if (temp->right == NULL ) {
					newNode->parent = temp;
					temp->right = newNode;
					break;
				} else {
					temp = temp->right;
				}
			} else {
				//printf("Duplicate insertion not allowed.");
				break;
			}
		}
	}
}
//**************************************************************************************************//
void inorder(b_node *t) {
	int c=0;
	if (t != NULL ) {
		inorder(t->left);
		printf("Filename: %40s \t\tFullPath: %40s\n", t->fileDesc->fileName,t->fileDesc->fullPath);
		inorder(t->right);
	}
}
//**************************************************************************************************//

int count_nodes_bst(b_node *n) {
	int c=0;
	if (n == NULL)
		return c;
	else {
		c=1+count_nodes_bst(n->left)+count_nodes_bst(n->right);
	}
}
//**************************************************************************************************//
void displayList() {
	b_node *temp = bstRoot;
	if (bstRoot == NULL ) {
		//printf("\nEmpty tree\n");
	} else {
		inorder(temp);
	}
}
//**************************************************************************************************//
b_node* search(b_node *t, char *data) {
	if (t == NULL || (strcmp(data, t->fileDesc->fullPath) == 0)) {
		return t;
	} else {
		if (strcmp(data, t->fileDesc->fullPath) < 0) {
			return search(t->left, data);
		} else {
			return search(t->right, data);
		}
	}
}
//**************************************************************************************************//
char* searchResult(char *data, char *output_file) {
	//char s[100];
	b_node *t = bstRoot;
	b_node *temp = search(t, data);
	
	if (temp == NULL ) {
		FILE *fp = fopen(output_file, "wb");
		fwrite("", 0, 1, fp);
		sprintf(outString, "%s %s", "searchfile_FAILURE", ERR_VFS_SEARCHFILE_01);/////////Check error code
		fclose(fp);		
		return outString;		
	} else {
		FILE *fp = fopen(output_file, "wb");
		if(fp == NULL){
			sprintf(outString, "%s %s", "searchfile_FAILURE", ERR_VFS_SEARCHFILE_03);
			return outString;	
		}
		fwrite("File Path :",strlen("File Path :"), 1, fp);
		fwrite(temp->fileDesc->fullPath,strlen(temp->fileDesc->fullPath), 1, fp);
		fclose(fp);
		return "searchfile_SUCCESS 1";
	}
}
//**************************************************************************************************//

void transplant(b_node *u, b_node *v) {
	if (u->parent == NULL ) {
		bstRoot = v;
	} else {
		if (u == u->parent->left) {
			u->parent->left = v;
		} else {
			u->parent->right = v;
		}
	}
	if (v != NULL ) {
		v->parent = u->parent;
	}
}
//**************************************************************************************************//

b_node* findMin(b_node *m) {
	while (m->left != NULL ) {
		m = m->left;
	}
//	printf("%s", m->fileDesc->fullPath);
	return m;
}
//**************************************************************************************************//
void del(char *data) {

	b_node* elem;
	b_node* successor = NULL;

	elem = search(bstRoot, data);

	if (elem != NULL ) {
	//	printf("\nElement is present\n");

		if (elem->left == NULL ) {
			transplant(elem, elem->right);
		} else {
			if (elem->right == NULL ) {
				transplant(elem, elem->left);
			} else {
				successor = findMin(elem->right);
				if (successor->parent != elem) {
					transplant(successor, successor->right);
					successor->right = elem->right;
					successor->right->parent = successor;
				}
				transplant(elem, successor);
				successor->left = elem->left;
				successor->left->parent = successor;
				successor->parent = elem->parent;

			}
		}
		free(elem);
	} else {
		//printf("\nElement is not present\n");
	}
}
//**************************************************************************************************//
void mount_bst() {
	
	
	int i;
	//printf("FD_list paths are : \n");

	for (i = 0; i < h->usedFds; i++) {
		
		insert(&array[i]);
		
	}
	

//	displayList();
}

//**************************************************************************************************//
void unmount_bst(b_node *t)
{
	if(t==NULL)
		return;
	unmount_bst(t->left);
	unmount_bst(t->right);
	free(t);
}

//**************************************************************************************************//
//**************************************************************************************************//
/*char* search_bst(char *P1, char*P2){
	return searchResult(P1, P2);
}*/


